<template>
  <div>
    <div class="head">
      <div class="title">{{ $t($route.meta.title) }}</div>
      <Dropdown trigger="click" class="other-column nav-dropdown">
        <div class="language">
          <img class="icon" src="@/assets/images/home/top_nav_language.svg" alt="" />
          <div class="type">
            <span>{{ userLanguage.text }}</span>
            <span>/</span>
            <span>{{ userCurrency.code }}</span>
            <Icon type="ios-arrow-down"></Icon>
          </div>
        </div>
        <DropdownMenu class="fq-dropdown-menu lang-dropdown-menu" slot="list">
          <div class="select-list-wrap lang-select">
            <span class="title">
              {{ $t("header.language") }}
            </span>
            <div class="select-list">
              <div class="select-list-item" v-for="item in langList" :key="item.text" @click="onUserLang(item)"
                :class="item.code == userLanguage.code ? 'select-active' : ''">
                <span>{{ item.text }}</span>
              </div>
            </div>
          </div>
          <div class="select-list-wrap rate-select">
            <span class="title">
              {{ $t("header.legalTender") }}
            </span>
            <div class="select-list">
              <div class="select-list-item" v-for="item in currencyList" :key="item.text" @click="onUserCurrency(item)"
                :class="item.code == userCurrency.code ? 'select-active' : ''">
                <span> {{ item.code }}</span>
              </div>
            </div>
          </div>
        </DropdownMenu>
      </Dropdown>
    </div>
    <div class="sub-title">{{ $t('promote.alliance_desc') }}</div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { editorMyInfo } from "@/api/user";
export default {
  data() {
    return {

    }
  },
  computed: {
    ...mapState({
      langList: (state) => {
        // 语言列表
        let list = [];
        if (state.config.config) {
          list = state.config.config.langList;
        }
        return list;
      },
      currencyList: (state) => {
        // 币种列表
        let list = [];
        if (state.config.config) {
          list = state.config.config.currencyList;
        }
        return list;
      },
      userLanguage: (state) => state.user.userLanguage,
      userCurrency: (state) => state.user.userCurrency,
    }),
  },
  methods: {
    onUserLang(info) {
      localStorage.setItem("user_language", JSON.stringify(info));
      this.$store.commit("setUserLanguage", info);
      if (this.userInfo) {
        editorMyInfo({
          lang: info.code,
        }).then(() => {
          window.location.replace(window.location.href);
        });
      } else {
        window.location.replace(window.location.href);
      }
    },
    onUserCurrency(info) {
      localStorage.setItem("user_currency", JSON.stringify(info));
      this.$store.commit("setUserCurrency", info);
      if (this.userInfo) {
        editorMyInfo({
          currencyCode: info.code,
        }).then(() => {
          window.location.replace(window.location.href);
        });
      } else {
        window.location.replace(window.location.href);
      }
    },
  }
}
</script>
<style lang="less" scoped>
.head {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .title {
    font-family: Open Sans, Open Sans;
    font-weight: 600;
    font-size: 24px;
    color: #1E1714;
    line-height: 28px;
    margin-bottom: 12px;
  }

  .other-column {
    .language {
      border-radius: 22px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      font-family: PingFang HK, PingFang HK;
      font-weight: 500;
      font-size: 19px;
      color: #AEB0B9;
      line-height: 22px;
      text-align: left;
      font-style: normal;
      text-transform: none;

      .icon {
        display: block;
        width: 24px;
      }

      .type {
        margin-left: 8px;
        // font-size: 14px;
        // font-weight: 600;
      }
    }

    :deep(.ivu-select-dropdown) {
      padding: 0;
    }

    .lang-dropdown-menu {
      padding: 30px 19px;
      width: 241px;
      height: 100%;
      background: #ffffff;
      box-shadow: 0px 0px 14px 0px rgba(34, 38, 43, 0.15);
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #ebeef5;

      .select-list-wrap {
        position: relative;

        &:first-child {
          &:after {
            background: rgba(0, 0, 0, 0);
          }
        }

        .title {
          display: block;
          padding: 0 16px;
          margin-bottom: 10px;
          font-size: 12px;
          font-weight: 600;
        }

        .select-list {
          min-width: 100px;
          max-height: 360px;
          overflow: auto;
          overflow-y: overlay;
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;

          &::-webkit-scrollbar {
            width: 2px;
            height: 1px;
          }

          &-item {
            width: 98px;
            height: 32px;
            background: #ffffff;
            border-radius: 4px 4px 4px 4px;
            border: 1px solid #dcdfe6;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 12px;
            font-weight: 600;
            margin-bottom: 8px;

            &:hover {
              color: @primary-color;
              border-color: @primary-color;

              .icon {
                color: @primary-color;
              }
            }

            .img {
              display: block;
              width: 16px;
              margin-right: 4px;
            }
          }

          .select-active {
            color: @primary-color;
          }
        }
      }
    }
  }
}

.sub-title {
  font-family: Open Sans, Open Sans;
  font-weight: 600;
  font-size: 20px;
  color: #767B93;
  line-height: 23px;
  margin-bottom: 32px;
}
</style>
